﻿using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using System;
using System.Collections;
using System.Data;

namespace DevTeam
{

    public partial class TopluGuncelleme : DevPopupForm
    {
        private string formName;
        private GridControl sourceGrid;
        GridEditorCollection gridEditors = new GridEditorCollection();

        public TopluGuncelleme(string formName, GridControl sourceGrid)
        {
            InitializeComponent();
            this.formName = formName;
            this.sourceGrid = sourceGrid;
        }

        private void InitializeEditors()
        {
            DataTable dtGridUpdateInfo = Session.DataManager.ExecuteDataTable("GetTopluGuncelleme", sourceGrid.DefaultView.Name, formName);

            //string sql = "select col.* from Grid g inner join GridColumn  col on g.GridId=col.GridId  inner join Form f on f.FormId = g.FormId where f.Name=@FormName and g.Name=@GridName order by VisibleIndex";

            //DbCommand cmd = dm.GetSqlStringCommand(sql);
            //dm.AddInParameter(cmd, "@FormName", DbType.String, formName);
            //dm.AddInParameter(cmd, "@GridName", DbType.String, sourceGrid.DefaultView.Name);
            //DataTable dtGridUpdateInfo = dm.ExecuteDatatable(cmd);

            foreach (DataRow dr in dtGridUpdateInfo.Rows)
            {
                RepositoryItem item = null;

                if (dr["RepositoryId"] != DBNull.Value)
                    item = RepositoryManager.Get((int)dr["RepositoryId"]);
                else                    
                    switch (dr["RIType"].ToString())
                    {
                        case "TextEdit":
                        case "Numeric2":
                        case "Percentage":
                            item = new RepositoryItemTextEdit();
                            break;
                        case "Datetime":
                            item = new RepositoryItemDateEdit();
                            break;
                        case "CalcEdit":
                            item = new RepositoryItemCalcEdit();
                            break;
                        case "MemoEdit":
                            item = new RepositoryItemMemoEdit();
                            break;
                        default:
                            item = new RepositoryItemTextEdit();
                            break;
                    }

                string caption;
                if (dr["Caption"].ToString() != "")
                {
                    caption = dr["Caption"].ToString();
                }
                else
                {
                    caption = dr["FieldName"].ToString();
                }
                gridEditors.Add(item, caption, dr["FieldName"].ToString());
            }

            gcPasteTool.DataSource = gridEditors;
        }

        private void SetColumnValue()
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sourceGrid.DefaultView;
            if (view.SelectedRowsCount > 0)
            {
                for (int i = 0; i < view.SelectedRowsCount; i++)
                {
                    DataRow row = view.GetDataRow(view.GetSelectedRows()[i]);
                    foreach (GridEditorItem item in gridEditors)
                    {
                        if (item.Status)
                        {
                            row[item.ColumnName] = DBNull.Value;
                        }
                        else
                        {
                            if (item.EditValue != null && item.EditValue.ToStr() != String.Empty && !view.Columns[item.ColumnName].ReadOnly)
                            {
                                try
                                {
                                    row[item.ColumnName] = item.EditValue;
                                }
                                catch
                                { 
                                }
                            }
                        }
                    }
                }

            }
        }

        private void frmPaste_Load(object sender, EventArgs e)
        {
            
            InitializeEditors();
            this.colStatus.Visible = false;

            gvPasteTool.OptionsView.ColumnAutoWidth = true;
            gvPasteTool.OptionsView.EnableAppearanceEvenRow = true;
            gvPasteTool.OptionsView.ShowAutoFilterRow = true;

            btnCancel.ItemClick += btnCancel_ItemClick;
            btnOk.ItemClick += btnOk_ItemClick;
        }

        void btnOk_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetColumnValue();
            this.Close();
        }

        void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void grvPasteInfo_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.colEditValue)
            {
                GridEditorItem item = gvPasteTool.GetRow(e.RowHandle) as GridEditorItem;
                if (item != null)
                {
                    e.RepositoryItem = item.RepositoryItem;
                    if (e.RepositoryItem is RepositoryItemGridLookUpEdit)
                        (e.RepositoryItem as RepositoryItemGridLookUpEdit).TextEditStyle = TextEditStyles.Standard;
                }
            }
        }

    }


    public class GridEditorItem
    {
        string columnCaption;
        string columnName;
        object editValue = null;
        bool status = false;
        RepositoryItem repositoryItem;

        public GridEditorItem(RepositoryItem repositoryItem, string columnCaption, string columnName)
        {
            this.repositoryItem = repositoryItem;
            this.columnCaption = columnCaption;
            this.columnName = columnName;
        }

        public string ColumnCaption
        {
            get { return columnCaption; }
        }

        public string ColumnName
        {
            get { return columnName; }
        }

        public object EditValue
        {
            get { return editValue; }
            set { editValue = value; }
        }

        public bool Status
        {
            get { return status; }
            set { status = value; }
        }

        public RepositoryItem RepositoryItem
        {
            get { return repositoryItem; }
        }
    }

    public class GridEditorCollection : ArrayList
    {
        public GridEditorCollection()
        {
        }

        public new GridEditorItem this[int index]
        {
            get
            {
                return base[index] as GridEditorItem;
            }
        }

        public void Add(RepositoryItem repositoryItem, string columnCaption, string columnName)
        {
            base.Add(new GridEditorItem(repositoryItem, columnCaption, columnName));
        }
    }
}